package com.weibo.click;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public final class InMemJobServices implements Serializable{
	private static final long serialVersionUID = 5216546407946170724L;
	private final static InMemJobServices instance = new InMemJobServices(); 
	public final static ConcurrentHashMap<String,String> weiboLiseners = new ConcurrentHashMap<String,String>();	
	//存放问候信息的links
	public final static ConcurrentHashMap<String,String>    otherInfo = new ConcurrentHashMap<String,String>();			
	//存放书籍的电子书links
	public final static ConcurrentLinkedQueue<BookLink>    bookLink = new ConcurrentLinkedQueue<BookLink>();			
	//记录全部weibo的link，消息的唯一编号
	public final static ConcurrentHashMap<String,String> weiboLink = new ConcurrentHashMap<String,String>();	
	//存储全部初始真相文件信息,文件名作为key
	public final static ConcurrentHashMap<String,String> msgMap=new ConcurrentHashMap<String,String>();
	//存储全部用于发送的真相文件信息,文件名作为key
	public final static ConcurrentHashMap<String,String> allSendMessages=new ConcurrentHashMap<String,String>();
	
	private final static Lock lck=new ReentrantLock();
	private final static Lock verifyCodeLock=new ReentrantLock();
	private final static Lock otherCodeLock=new ReentrantLock();
	private final static Lock bookLinLock=new ReentrantLock();
	private final static Lock msgLock=new ReentrantLock();
	
	
	private InMemJobServices() 
	{ 
	} 
	/*关键的算法，随机返回一个真相文件的内容*/
	public  String retriveRandomTruthFileFromBuffer(int messageSequence){
	      String contents="";
		  //int size=allSendMessages.size();
		  Object[] msg=(Object[])allSendMessages.values().toArray();
		  int len=allSendMessages.size();
		  int idx=Math.abs(messageSequence % len);
		  contents=new String(msg[idx].toString());
		  //logger.info("true message is: "+contents+idx);
	      return contents;
	}
	public  String getRandomOtherInfo(int k){
		String contents="";
		int size=otherInfo.size();
		Object[] msg=(Object[])otherInfo.values().toArray();
		int len=otherInfo.size();
		int idx=Math.abs(k % len);
		contents=new String(msg[idx].toString());
		return contents;
	}
	public  String getRandomOtherInfoOld(){
		  String contents="";
		  otherCodeLock.lock();
		  try{
			  int size=otherInfo.size();
			  Object[] msg=(Object[])otherInfo.values().toArray();
			  Random rnd=new Random(System.currentTimeMillis());
			  int idx=rnd.nextInt(size-1);
			  contents=new String(msg[idx].toString());
			  //logger.info("OtherInfo=="+contents);
		  }catch(Exception e){
			  //logger.error(e);  
		  }finally{
			  otherCodeLock.unlock();
		  }
		  return contents;
	}
	public  String getRandomLink(){
		  String contents="";
		  bookLinLock.lock();
		  try{
			  int size=otherInfo.size();
			  Object[] msg=(Object[])otherInfo.values().toArray();
			  Random rnd=new Random(System.currentTimeMillis());
			  int idx=rnd.nextInt(size-1);
			  contents=new String(msg[idx].toString());
			  //logger.info("OtherInfo=="+contents);
		  }catch(Exception e){
			  //logger.error(e);  
		  }finally{
			  bookLinLock.unlock();
		  }
		  return contents;
	}
	/*真相文件修改后，内容会自动被替换*/
	public void addMsgFromFile(String key,String value){
		msgMap.put(key, value);
	}
	
	public void delEmptyMsgFile(String key){
		msgMap.remove(key);
	}
	public final static InMemJobServices getInstance(){ 
		return instance; 
	} 	

}
